'use strict';
const Controller = require('egg').Controller;

class ApplicationController extends Controller {

  /**
     *@api {post} /app/token 根据code + salt换算token，过期需要重新生成
     *@apiName token
     *@apiGroup Application
     *@apiParam {string} code AppCode
     *@apiParam {string} salt AppSalt
     *@apiSuccess {int} errcode  错误码
     *@apiSuccess {string} message 提示语
     *@apiSuccess {Object} data 数据对象
     *@apiSuccess {string} data.desc 描述
     *@apiSuccess {string} data.expire 过期日期时间
     *@apiSuccess {int} data.status 用户状态 0:作废 1:正常
     *@apiSuccess {string} data.token 验证token
     */
  async token() {
    const { ctx, service } = this;
    const body = await ctx.multipartToJson() || ctx.request.body;
    const { code, salt } = body;
    if (!code) {
      return ctx.json({ errcode: ctx.ERRCODE.DATA_ERROR, message: 'Code不能为空' });
    }
    if (!salt) {
      return ctx.json({ errcode: ctx.ERRCODE.DATA_ERROR, message: 'Salt不能为空' });
    }
    const reponse = await service.application.token(code, salt);
    ctx.json(reponse);
  }

}

module.exports = ApplicationController;
